package com.zzj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zzj.entity.Category;
import com.zzj.mapper.CategoryMapper;
import com.zzj.model.dto.CategoryCreateDto;
import com.zzj.model.dto.QueryListDto;
import com.zzj.service.ICategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzj.utils.QueryUtil;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 分类表 服务实现类
 * </p>
 *
 * @author zzj
 * @since 2023-07-17
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements ICategoryService {

    @Override
    public IPage<Category> queryList(QueryListDto in) {
        QueryWrapper<Category> queryWrapper = QueryUtil.paramsWrapper(in.getAndParams(), in.getOrParams(), in.getOrderParams());
        Page<Category> categoryPage = new Page<>(in.getPageNo(), in.getPageSize());
        return this.getBaseMapper().selectPage(categoryPage, queryWrapper);
    }

    @Override
    public Category create(CategoryCreateDto in) {
        Category category = new Category();
        category.setName(in.getName());
        this.save(category);
        return category;
    }

    @Override
    public Category findCategory(String name) {
        return this.getOne(Wrappers.<Category>lambdaQuery().eq(Category::getName, name), false);
    }
}
